package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.SchoolClassesjk;
import com.ruoyi.system.api.domain.SchoolStudent;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISchoolClassesjkService;
import com.ruoyi.system.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchoolClassesjkServiceImpl implements ISchoolClassesjkService {
    @Autowired
    private SchoolClassesjkMapper schoolClassesjkMapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public SchoolClassesjk selectSchoolClassesjkById(Long id) {
        return schoolClassesjkMapper.selectSchoolClassesjkById(id);
    }

    @Override
    public List<SchoolClassesjk> selectSchoolClassesjkList(SchoolClassesjk schoolClasses) {
        return schoolClassesjkMapper.selectSchoolClassesjkList(schoolClasses);
    }

    @Override
    public int insertSchoolClassesjk(SchoolClassesjk schoolClasses) {
        schoolClasses.setCreateTime(DateUtils.getNowDate());
        List<SchoolClassesjk> classList = schoolClassesjkMapper.selectSchoolClassesjkList(new SchoolClassesjk());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClassesjk obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASSJK:DATA");
        redisService.setCacheMap("CLASSJK:DATA", classMap);
        if (null!=schoolClasses.getSchoolId()) {
            redisService.delCacheMap("CLASSJK:DATA:"+schoolClasses.getSchoolId());
            redisService.setCacheMap("CLASSJK:DATA:"+schoolClasses.getSchoolId(), classMap);
        }
        return schoolClassesjkMapper.insertSchoolClassesjk(schoolClasses);
    }

    @Override
    public int updateSchoolClassesjk(SchoolClassesjk schoolClasses) {
        List<SchoolClassesjk> classList = schoolClassesjkMapper.selectSchoolClassesjkList(new SchoolClassesjk());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClassesjk obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASSJK:DATA");
        redisService.setCacheMap("CLASSJK:DATA", classMap);
        if (null!=schoolClasses.getSchoolId()) {
            redisService.delCacheMap("CLASSJK:DATA:"+schoolClasses.getSchoolId());
            redisService.setCacheMap("CLASSJK:DATA:"+schoolClasses.getSchoolId(), classMap);
        }
        return schoolClassesjkMapper.updateSchoolClassesjk(schoolClasses);
    }

    @Override
    public void checkCanDelete(Long[] ids)
    {
        for (Long id:ids) {
            SchoolStudent query = new SchoolStudent();
            query.setClassesId(id);
            List<SchoolStudent> studentList = schoolStudentMapper.selectSchoolStudentList(query);
            if (null!=studentList&&studentList.size()>0) {
                throw new CustomException("不允许删除有学生的班级");
            }
        }
    }

    @Override
    public int deleteSchoolClassesjkByIds(Long[] ids) {
        List<SchoolClassesjk> classList = schoolClassesjkMapper.selectSchoolClassesjkList(new SchoolClassesjk());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClassesjk obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        for (Long id:ids) {
            redisService.delCacheMap("CLASSJK:DATAB:" + id);
        }
        redisService.delCacheMap("CLASSJK:DATA");
        if (null!=classList&&classList.size()>0) {
            redisService.delCacheMap("CLASSJK:DATA:"+classList.get(0).getSchoolId());
        }
        return schoolClassesjkMapper.deleteSchoolClassesjkByIds(ids);
    }

    @Override
    public int deleteSchoolClassesById(Long id) {
        List<SchoolClassesjk> classList = schoolClassesjkMapper.selectSchoolClassesjkList(new SchoolClassesjk());
        Map<String, String> classMap = new HashMap<>();
        for (SchoolClassesjk obj:classList) {
            classMap.put(obj.getId()+"", JSON.toJSONString(obj));
        }
        redisService.delCacheMap("CLASSJK:DATAB:" + id);
        redisService.delCacheMap("CLASSJK:DATA");
        if (null!=classList&&classList.size()>0) {
            redisService.delCacheMap("CLASSJK:DATA:"+classList.get(0).getSchoolId());
        }
        return schoolClassesjkMapper.deleteSchoolClassesjkById(id);
    }
}
